package org.shan.hangtian.ui;

import org.shan.hangtian.DeviceClient;
import org.shan.hangtian.DeviceControl;
import org.shan.hangtian.server.session.SessionManager;
import org.shan.hangtian.tp.RunMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainFrame extends JFrame {
    private Logger logger = LoggerFactory.getLogger(MainFrame.class);

    public static Integer SCREEN_W;// 屏幕宽

    public static Integer SCREEN_H;// 屏幕高（去任务栏）

    private JPanel mainPanel;

    private JLabel setting, center, connectState;

    private DeviceControl deviceControl;

    private RunMode currMode;

    static {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        SCREEN_W = screenSize.width;
        SCREEN_H = screenSize.height;
    }

    public MainFrame() {
        currMode = RunMode.MODE_1;

        setTitle("脑疲劳恢复仪");
        setIconImage(new ImageIcon(ClassLoader.getSystemResource("imgs/icon2.png")).getImage());
        setBounds((SCREEN_W - 800) / 2, (SCREEN_H - 600) / 2, 800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        {
            JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 3));
            menuPanel.setBackground(new Color(36, 48, 64));
            menuPanel.setPreferredSize(new Dimension(0, 40));
            {
                connectState = new JLabel(new ImageIcon(ClassLoader.getSystemResource("imgs/disconnect.png")));
                menuPanel.add(connectState);

                setting = new JLabel("设置");
                setting.setFont(new Font("黑体", Font.BOLD, 16));
                setting.setForeground(new Color(250, 250, 250));
                setting.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//                menuPanel.add(setting);
            }
            mainPanel.add(menuPanel, BorderLayout.NORTH);

            center = new JLabel();
            center.setIcon(new ImageIcon(ClassLoader.getSystemResource("imgs/mode1.jpg")));
            center.setLayout(new GridLayout(1, 1));
            center.setBorder(new EmptyBorder(0, 0, 0, 0));
            center.setBackground(null);
            center.setOpaque(false);
            {
                CenterPanel centerPanel = new CenterPanel(currMode, center, false);
                center.add(centerPanel);
            }
            mainPanel.add(center, BorderLayout.CENTER);
        }
        setContentPane(mainPanel);

        addListeners();
    }

    private void addListeners() {
        setting.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                SettingDialog settingDialog = new SettingDialog();
                settingDialog.setVisible(true);
            }
        });

        SessionManager.getInstance().setSessionLis(new SessionManager.SessionLis() {
            @Override
            public void online() {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        connectState.setIcon(new ImageIcon(ClassLoader.getSystemResource("imgs/connect.png")));
                        connectState.updateUI();
                    }
                });
            }

            @Override
            public void offline() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            deviceControl.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        connectState.setIcon(new ImageIcon(ClassLoader.getSystemResource("imgs/disconnect.png")));
                        connectState.updateUI();
                    }
                });
            }

            @Override
            public void modeChange(RunMode runMode) {
                logger.debug("收到设备模式：" + runMode);
                if (currMode == runMode)
                    return;

                currMode = runMode;
                CenterPanel centerPanel = new CenterPanel(runMode, center, true);

                ImageIcon imageIcon;
                if (RunMode.MODE_1.equals(runMode)) {
                    imageIcon = new ImageIcon(ClassLoader.getSystemResource("imgs/mode1.jpg"));
                } else if (RunMode.MODE_2.equals(runMode)) {
                    imageIcon = new ImageIcon(ClassLoader.getSystemResource("imgs/mode2.jpg"));
                } else {
                    imageIcon = new ImageIcon(ClassLoader.getSystemResource("imgs/mode3.jpg"));
                }

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        center.setIcon(imageIcon);
                        center.removeAll();
                        center.add(centerPanel);

                        center.updateUI();
                    }
                });
            }
        });
    }

    public void initServer() {
        deviceControl = new DeviceClient();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    deviceControl.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.initServer();
        frame.setVisible(true);
    }
}
